package qms

import grails.converters.*

class IndexController {

	def index() { }

	def orgTree = {
		
		if (params.do_action == 'read') {

			if (params.node == 'root') {
				def org = Organisation.read(1)
				def orgJSON = [
					root: [
						id: "ORG-1",
						text: org.name,
						leaf: false,
						expanded: org.units.size() != 0,
						root: org.units.collect { unit ->
							[
								id: "UNIT-${unit.id}",
								text: unit.name,
								leaf: false,
								expanded: unit.projects.size() != 0,
								root: unit.projects.collect { project ->
									[
										id: "PROJ-${project.id}",
										text: project.name,
										leaf: false,
										expanded: project.employees.size() != 0,
										root: project.employees.collect { employee ->
											[
												id: "EMPL-${employee.id}",
												text: employee.name + " " + employee.surName,
												leaf: true
											]
										}
									]
								}
							]
						}
					]
				]

				render orgJSON as JSON
	  
			} else {
				response.status = 404
				return
			}

		} else if (params.do_action == 'destroy') {

			println 'orgTree destroy'
			println params
			println request.JSON

			if (!request.JSON.id) {
				response.status = 404
				return
			}

			def splitedNode = request.JSON.id.split("-")
			def modelName = splitedNode[0]
			def modelIndex = splitedNode[1]

			switch ( modelName ) {

				case "UNIT":
					def unit = Unit.read(modelIndex)
					unit.delete()
					render(contentType: "application/json") {
						root(name: unit.name, id: unit.id, description: unit.description)
					}
					break

				case "PROJ":
					def project = Project.read(modelIndex)
					project.delete()
					render(contentType: "application/json") {
						root(name: project.name, id: project.id, description: project.description)
					}
					break

				case "EMPL":
					def empl = Employee.read(modelIndex)
					empl.delete()
					render(contentType: "application/json") {
						root(name: empl.name, id: empl.id, surName: empl.surName, email: empl.email, birthDay: empl.birthDay, dateEmployed: empl.dateEmployed)
					}
					break

				default:
					response.status = 404
					return
			}

		} else if (params.do_action == 'update') {

			println 'orgTree update'
			println params
			println request.JSON
			response.status = 404
			return

		} else if (params.do_action == 'create') {

			println 'orgTree create'
			println params
			println request.JSON
			
			if (!request.JSON.parentId) {
				response.status = 404
				return
			}

			def splitedNode = request.JSON.parentId.split("-")
			def modelName = splitedNode[0]
			def modelIndex = splitedNode[1]

			switch ( modelName ) {
				case "ORG":

					def org = Organisation.read(modelIndex)
					def newUnit = new Unit()
					newUnit.name = request.JSON.name
					newUnit.description = request.JSON.description
					newUnit.projects = []
					org.addToUnits(newUnit)

					if (!newUnit.validate()) {
						newUnit.errors.each {
							println it
						}
						response.status = 500
						return
					}

					newUnit.save()

					def unitJSON = [
						root: [
							parentId: "ORG-1",
							id: "UNIT-${newUnit.id}",
							text: newUnit.name,
							leaf: false,
						]
					]
					render unitJSON as JSON
					break

				case "UNIT":
					def unit = Unit.read(modelIndex)
					def newProject = new Project()
					newProject.name = request.JSON.name
					newProject.description = request.JSON.description
					newProject.employees = []
					unit.addToProjects(newProject)

					if (!newProject.validate()) {
						newProject.errors.each {
							println it
						}
						response.status = 500
						return
					}

					newProject.save()

					def projectJSON = [
						root: [
							parentId: request.JSON.parentId,
							id: "PROJ-${newProject.id}",
							text: newProject.name,
							leaf: false,
						]
					]
					
					render projectJSON as JSON
					break

				case "PROJ":
					def project = Project.read(modelIndex)
					def newEmployee = new Employee()
					newEmployee.name = request.JSON.name
					newEmployee.surName = request.JSON.surName
					newEmployee.email = request.JSON.email
					if (request.JSON.birthDay) {
						newEmployee.birthDay = Date.parse('yyyy-MM-dd', request.JSON.birthDay)
					}
					if (request.JSON.dateEmployed) {
						newEmployee.dateEmployed = Date.parse('yyyy-MM-dd', request.JSON.dateEmployed)
					}
					project.addToEmployees(newEmployee)

					if (!newEmployee.validate()) {
						newEmployee.errors.each {
							println it
						}
						response.status = 500
						return
					}

					newEmployee.save()

					def employeeJSON = [
						root: [
							parentId: request.JSON.parentId,
							id: "EMPL-${newEmployee.id}",
							text: newEmployee.name + " " + newEmployee.surName,
							leaf: true,
						]
					]
					
					render employeeJSON as JSON
					break
				default:
					response.status = 404
					return
			}

		} else {
			println 'orgTree unsupported do_action'
			println params
			response.status = 404
			return
		}
	}

	def organisation (request) {
		if (params.do_action == 'read') {
			println 'organisation read'
			println params
			println request.JSON

			def org = Organisation.read(params.id)
			if (org) {
				render(contentType: "application/json") {
					root(name: org.name, id: org.id, description: org.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'update') {
			println 'organisation update'
			println params
			println request.JSON

			def org = Organisation.read(request.JSON.id)
			if (org) {
				org.name = request.JSON.name
				org.description = request.JSON.description
				org.save()
				println org
				render(contentType: "application/json") {
					root(name: org.name, id: org.id, description: org.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'create') {
			println 'organisation create unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		} else if (params.do_action == 'destroy') {
			println 'organisation destroy unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		}
	}

	def unit (request) {
		if (params.do_action == 'read') {
			println 'unit read'
			println params
			println request.JSON

			def unit = Unit.read(params.int('id'))
			if (unit) {
				render(contentType: "application/json") {
					root(name: unit.name, id: unit.id, description: unit.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'update') {
			println 'unit update'
			println params
			println request.JSON

			def unit = Unit.read(request.JSON.id)
			if (unit) {
				unit.name = request.JSON.name
				unit.description = request.JSON.description
				unit.save()
				render(contentType: "application/json") {
					root(name: unit.name, id: unit.id, description: unit.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'create') {
			println 'unit create unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		} else if (params.do_action == 'destroy') {
			println 'unit destroy unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		}
	}

	def project (request) {
		if (params.do_action == 'read') {
			println 'project read'
			println params
			println request.JSON
			
			def proj = Project.read(params.int('id'))
			if (proj) {
				render(contentType: "application/json") {
					root(name: proj.name, id: proj.id, description: proj.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'update') {
			println 'project update'
			println params
			println request.JSON

			def proj = Project.read(request.JSON.id)
			if (proj) {
				proj.name = request.JSON.name
				proj.description = request.JSON.description
				proj.save()
				render(contentType: "application/json") {
					root(name: proj.name, id: proj.id, description: proj.description)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'create') {
			println 'project create unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		} else if (params.do_action == 'destroy') {
			println 'project destroy unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		}
	}

	def employee (request) {
		if (params.do_action == 'read') {
			println 'employee read'
			println params
			println request.JSON
			
			def empl = Employee.read(params.int('id'))
			if (empl) {
				render(contentType: "application/json") {
					root(name: empl.name, id: empl.id, surName: empl.surName, email: empl.email, birthDay: empl.birthDay, dateEmployed: empl.dateEmployed)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'update') {
			println 'employee update'
			println params
			println request.JSON

			def empl = Employee.read(request.JSON.id)
			if (empl) {
				empl.name = request.JSON.name
				empl.surName = request.JSON.surName
				empl.email = request.JSON.email
				empl.birthDay = Date.parse('yyyy-MM-dd', request.JSON.birthDay)
				empl.dateEmployed = Date.parse('yyyy-MM-dd', request.JSON.dateEmployed)
				empl.save()
				render(contentType: "application/json") {
					root(name: empl.name, id: empl.id, surName: empl.surName, email: empl.email, birthDay: empl.birthDay, dateEmployed: empl.dateEmployed)
				}
			} else {
				response.status = 404
				return
			}
		} else if (params.do_action == 'create') {
			println 'employee create unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		} else if (params.do_action == 'destroy') {
			println 'employee destroy unsupported'
			println params
			println request.JSON
			response.status = 404
			return
		}
	}
}
